var app = getApp();
var appData = app.globalData;
import {searchGoodsXCX} from '../../../Tools/HTTPOpertion.js';
Page({
  data: {
    page: 1,//页码
    Ddarr: [],    //关于订单返回的总数据
    condition:'',
    delivery:0, //默认全部
    hid: false,
    plahold:'',
    startJZ:false
  },
  onLoad: function (options) {
    this.setData({
      condition: options.condition
    })
    this.data.Ddarr = [];
    this.data.page = 1;
    this.getDdList();
  },
  //详情
  gmTap: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/active/makeGroupsOrderDetails/makeGroupsOrderDetails?id=' + e.currentTarget.dataset.id + '&goods_type=' + e.currentTarget.dataset.goods_type
    })
  },
  toFocus: function (e) {
    if (e.detail.value != '') {
      this.setData({
        hid: true,
      })
    }
  },
  toInput: function (e) {
    console.log(e)
    if (e.detail.value != '') {
      this.setData({
        hid: true,
      })
    } else {
      this.setData({
        hid: false,
      })
    }
  },
  toBlur: function () {
    this.setData({
      hid: false
    })
  },
  qkwenzi: function () {
    this.setData({
      condition: '',
      plahold: '搜索商品名称'
    })
  },
  //获取全部搜索结果
  getDdList: function () {
    var token = wx.getStorageSync("token");
    var condition = this.data.condition;
    var codeid = wx.getStorageSync("codeid") || appData.codeid;
    var delivery = this.data.delivery;
    var that = this;
    searchGoodsXCX({ token: token, page: that.data.page, rows: 10, condition: condition, areaId: codeid, delivery: delivery}).then(function (res) {
      var arr = res.data.list;
      for (var k in arr) {
        arr[k].group_price = fn(arr[k].group_price);
        arr[k].price = fn(arr[k].price);
        arr[k].ratio = Math.round(arr[k].group_price*fn(arr[k].ratio))/100;
      }
      function fn(a) {
        if (typeof a == 'string') { return a };
        var str = a / 100 + '';
        var i = str.indexOf('.');
        if (i != -1) {
          if (str.length != i + 3) {
            str += '0';
          }
        } else {
          str += '.00';
        }
        return str;
      };
      console.log(res.data.list)
      if (res.data.list.length != 0 && that.data.page == 1) {
        that.setData({
          Ddarr: arr,
          startJZ:true
        })
      } else if (res.data.list.length != 0 && that.data.page != 1) {
        that.setData({
          Ddarr: that.data.Ddarr.concat(res.data.list)
        })
      } else if (res.data.list.length == 0 && that.data.page == 1) {
        that.setData({
          Ddarr: arr,
          startJZ:true
        })
      }
    })
      .catch(function (err) {
        console.log(err)
        wx.showToast({
          title: err.message,
          duration: 2000
        })
      })
  },

  //下拉加载
  xijSoll: function () {
    this.setData({
      page: this.data.page + 1
    });
    this.getDdList();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.Ddarr = [];
    this.data.startJZ=false;
    this.data.page = 1;
    this.getDdList();
    console.log(123456)
    wx.stopPullDownRefresh()
  },
  aa: function () {
    this.data.Ddarr = [];
    this.data.startJZ = false;
    this.data.page = 1;
    this.getDdList();
    console.log(123456)
    wx.stopPullDownRefresh()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  //滑动切换
  swiperTab: function (e) {
    console.log(e)
    // console.log(e.detail.current)
    // var that = this;
    this.setData({
      delivery: e.detail.current,
      Ddarr: [],
      startJZ:false,
      page: 1,
    });
    this.getDdList();
  },
  //点击切换
  clickTab: function (e) {
    this.setData({
      delivery: e.target.dataset.current,
      Ddarr: [],
      startJZ:false,
      page: 1,
    })
    this.getDdList();
  },
  toOrderDetails: function (e) {
    var type1 = e.currentTarget.dataset.type;
    var id = e.currentTarget.dataset.id;
    if (type1== 1) {
      wx.navigateTo({
        url: '/pages/active/makeGroupsDetails/makeGroupsDetails?id=' + id,
      })
    } else if (type1== 2) {
      wx.navigateTo({
        url: '/pages/active/miaoshaDetails/miaoshaDetails?id=' + id + '&times=123&merchantName=秒杀详情',
      })
    } else if (type1== 3) {
      wx.navigateTo({
        url: '/pages/active/bargainOwnPage/bargainOwnPage?id='+ id,
      })
    }
  },
  forSubmit: function (e) {
    if(appData.searchUserData.indexOf(e.detail.value) < 0 && e.detail.value || appData.searchUserData.indexOf(e.detail.value) > 9 && e.detail.value) {
      appData.searchUserData.unshift(e.detail.value);
      wx.setStorageSync("searchUserData", appData.searchUserData);
    }
    this.setData({
      condition: e.detail.value
    })
    this.getDdList();
  },
  goToHome:function(){
    wx.navigateBack({
      delta:2
    })
  }
})
